<?php

require_once 'PHPUnit/Extensions/SeleniumTestCase.php';

class WpmagickTest extends PHPUnit_Extensions_SeleniumTestCase {

	/*
	* Check that settings.php file exists. If not, creates a boilerplate one.
	* Check that settings.php file appears to have had its constants defined,
	* else prompts user.
	* Sets up the Selenium variables.
	*/
	function setUp() {
		// Try to ensure settings.php exists and is set up.
		if( file_exists( dirname( __FILE__ ).'/settings.php' ) ) {
			require_once dirname( __FILE__ ).'/settings.php';
			if( !( defined( 'BROWSER' ) && ( defined( 'TESTSITEURL' ) && ( defined( 'USERNAME' ) && defined( 'PASSWORD' ) ) ) ) ) {
				exit("One or more of the constants in your settings.php file has not ".
					"been set. Please edit settings.php with the appropriate values for ".
					"your test rig, and then try running the tests again.\n");
			}
		} else {
			$settings = fopen( dirname( __FILE__ ).'/settings.php', 'w' );
			$settings_contents = <<<EOT
<?php
/**
* wpmagickpix test settings
*
* Use the settings in this file to ensure that wpmagickpix tests will look for your WordPress test instance in the correct
* place and will also use the desired browser.
*/
function defineConstants() {
	define('BROWSER', ''); # Enter the name of the browser you want to use for testing. Typically either '*safari' or '*firefox'.
	define('TESTSITEURL', ''); # Set this to the URL of your WordPress test site, e.g. 'http://localhost/mytestsite/'. Include the trailing slash!
	define('USERNAME', ''); # Set this to the username of the account you want to use to test WpMagickPix on your WordPress test site, e.g. 'admin'.
	define('PASSWORD', ''); # Set this to the password of the account you want to use to test WpMagickPix on your WordPress test site, e.g. 'admin'.
}
defineConstants();
?>
EOT;
		fwrite( $settings, $settings_contents );
		exit( "This appears to be the first time you've run the tests. A ".
			"settings.php file has been created for you. Please edit settings.php ".
			"with the appropriate values for your test rig, and then try running the ".
			"tests again.\n" );
		}

		// Set up Selenium RC environment.
		$this->setBrowser( BROWSER );
		$this->setBrowserUrl( TESTSITEURL );
	}

	function toggleMediaMenuOpen() {
		$this->click( '//li[@id="menu-media"]/div[@class="wp-menu-toggle"]' );
		$this->assertTrue( $this->isVisible( '//li[@id="menu-media"]/div[@class="wp-submenu"]' ));
	}

	function toggleMediaMenuClosed() {
		$this->click( '//li[@id="menu-media"]/div[@class="wp-menu-toggle"]' );
		sleep( 1 );
		$this->assertFalse( $this->isVisible( '//li[@id="menu-media"]/div[@class="wp-submenu"]' ));
	}

	function test() {
		$this->open( 'wp-admin' );

		// If not logged in, log in.
		if ( !$this->isElementPresent( '//a[@title="Log Out"]' ) ) {
			$this->type( '//input[@id="user_login"]', USERNAME );
			$this->type( '//input[@id="user_pass"]', PASSWORD );
			$this->clickAndWait( '//input[@id="wp-submit"]' );
		}

		// Check wpmagickpix activates successfully, deactivating it first if need be.
		$this->clickAndWait( '//a[@class="wp-has-submenu menu-top" and @href="plugins.php"]' );
		if ( $this->isElementPresent( '//table[@id="all-plugins-table"]//tr[@class="active"]/td[@class="plugin-title"]/strong[.="wpmagickpix"]' ) ) {
			$this->clickAndWait( '//table[@id="all-plugins-table"]//td[@class="plugin-title"]/strong[.="wpmagickpix"]/parent::*/parent::*/following-sibling::*[1]//a[.="Deactivate"]' );
		}
		$this->clickAndWait( '//table[@id="all-plugins-table"]//tr[@class="inactive"]/td[@class="plugin-title"]/strong[.="wpmagickpix"]/parent::*/parent::*/following-sibling::*[1]//a[@class="edit" and .="Activate"]' );
		$this->assertTrue( $this->isElementPresent( '//table[@id="all-plugins-table"]//tr[@class="active"]/td[@class="plugin-title"]/strong[.="wpmagickpix"]' ) );

		// Check activated plugin isn't causing PHP errors.
		$this->assertFalse ( $this->isElementPresent( 'xpath=/html/body/b' ) );

		// Test that the Media admin menu item toggles correctly, and leave it open.
		$this->assertTrue( $this->isElementPresent( '//li[@id="menu-media"]/div[@class="wp-submenu"]' ) );
		if ( $this->isVisible( '//li[@id="menu-media"]/div[@class="wp-submenu"]' ) != TRUE ) {
			$this->toggleMediaMenuOpen();
			$this->toggleMediaMenuClosed();
			$this->toggleMediaMenuOpen();
		} else {
			$this->toggleMediaMenuClosed();
			$this->toggleMediaMenuOpen();
		}

		// Check that the wpmagickpix Media admin submenu item is present.
		$this->assertTrue( $this->isElementPresent( '//li[@id="menu-media"]/div[@class="wp-submenu"]/ul/li/a[.="Manage/upload media"]' ) );

		// Check that the WordPress default Media admin submenu items are not present.
		$this->assertFalse( $this->isElementPresent( '//li[@id="menu-media"]/div[@class="wp-submenu"]/ul/li/a[.="Library"]' ) );
		$this->assertFalse( $this->isElementPresent( '//li[@id="menu-media"]/div[@class="wp-submenu"]/ul/li/a[.="Add New"]' ) );

		// Check that the WordPress default Media admin menu item links to the same location as the wpmagickpix Media admin submenu item.
		$wpmp_admin_page_url = $this->getAttribute( '//li[@id="menu-media"]/div[@class="wp-submenu"]/ul/li/a[.="Manage/upload media"]@href' );
		$this->assertTrue( $this->getAttribute( '//li[@id="menu-media"]/div[@class="wp-menu-image"]//a@href' ) == $wpmp_admin_page_url );
		$this->assertTrue( $this->getAttribute( '//li[@id="menu-media"]/a@href' ) == $wpmp_admin_page_url );

		// Check that upload.php gets redirected to the same location as the wpmagickpix Media admin submenu item.
		$this->open( 'wp-admin/upload.php' );
		$this->assertTrue( $this->getLocation() == TESTSITEURL . 'wp-admin/' . $wpmp_admin_page_url );

		// Log out.
		$this->click( '//a[@title="Log Out"]' );
	}
}

?>
